//支付修改支付方式页面
$(function(){
	init();
});

var bankCities="";
var appBase;
var nbPremium=0;
function init(){
	
	var sessAppBase = sessionStorage.getItem("appBase");
	var sendData = JSON.parse(sessAppBase);

	console.log(sendData);

	var orderId = sendData.orderId;
	$("#orderId").text(orderId);
	padMarketing.ajaxNormal('editPayInit.action',sendData,
		function(result){
			var json = eval('(' + result.data + ')');
			console.log(json);
			appBase = json.app.appBase;
			nbPremium = appBase.nbPremium;
			var nbPaymentMethodName = appBase.nbPaymentMethodName;
			$("#nbPaymentMethodName").text(nbPaymentMethodName);
			var pmBaNo = appBase.pmBaNo; //银行卡号
			
			
			//判断用户原有支付方式
/*			if(appBase.nbPaymentMethod == '01'){
				
			};*/
		
			//新支付方式选择option
			var nbPaymentMethods = json.nbPaymentMethods;
			addOption('nbPaymentMethods','请选择','00');
			for(var i in nbPaymentMethods){
				var nbPaymentMethod = nbPaymentMethods[i];
				if(appBase.nbPaymentMethod == "02" && nbPaymentMethod.code == "02") {
					continue;
				}
				addOption('nbPaymentMethods',nbPaymentMethod.name,nbPaymentMethod.code);
			}
			
			//缴费银行账户开户行
			var pmBaBankCategoryId = appBase.pmBaBankCategoryId;
			var pmBankCategories = json.pmBankCategories;
			addOption('pmBankCategories','请选择');
			for(var i in pmBankCategories){
				var pmBankCategory = pmBankCategories[i];
				//默认第一个银行
				addOption('pmBankCategories',pmBankCategory.text,pmBankCategory.val);
			}
			
			//开户地省
			var pmBaProvince = appBase.pmBaProvince;
			var bankProvinces = json.bankProvinces;
			addOption('bankProvinces','请选择');
			for(var i in bankProvinces){
				var bankProvince = bankProvinces[i];
				addOption('bankProvinces',bankProvince.text,bankProvince.val);
			}
			
			//开户地市
			var pmBaCityName = appBase.pmBaCityName;
			var pmBaAreaCode = appBase.pmBaAreaCode;
			addOption('bankCities','请选择');
			addOption('bankCities',pmBaCityName,pmBaAreaCode);
			bankCities = json.bankCities;
			
			//同交费账户按钮
			$("#sameMoney1").click(function(){
				//点击按钮变色
				$(this).removeClass("btn-outline");
				$("#pmBankCategories").val(appBase.pmBaBankCategoryId); //银行
				$("#bankProvinces").val(appBase.pmBaProvince); //开户地
				selBankCity();
				$("#bankCities").val(appBase.pmBaAreaCode); //开户省市
				$("#pmBaNo").val(pmBaNo); //账号
			}); 
			
			
		},
		function(){
			$('#alertMsg_btn3').click(function(){
				window.location.href = '/pad_marketing/ordermanager/index.action';
			});
//			alertMsg("修改支付方式异常！");
		});
}

function addOption(select_id,objItemText,objItemValue,selected){
	var objSelect = document.getElementById(select_id);
	var new_opt = new Option(objItemText, objItemValue,false,selected);      
	objSelect.options.add(new_opt);
}

/**
 * 动态加载市
 */
function selBankCity(){
	
	$("#bankCities").html("");
	var bankProvince = $("#bankProvinces").val();
	for (var i in bankCities) {
		var bankCity = bankCities[i];
		if (bankProvince == bankCity.province) {
			addOption('bankCities',bankCity.city,bankCity.areaCode);
		}
	}
}

function saveEditPay(){
	
	//选择支付方式
	var nbPaymentMethod = $("#nbPaymentMethods").val();
	if('00'==nbPaymentMethod){
		alertMsg("请选择新支付方式！");
		return;
	}
	if('02'==nbPaymentMethod){
		$("#bankPay").hide();
		if(nbPremium>20000){
			alertMsg("在线支付的限额不能超过20000元");
			$("#nbPaymentMethods").val("00");
			return ;
		}
	}
	
	//判断开户行
	var pmBankCategories = $('#pmBankCategories option:selected') .val();
	if(pmBankCategories == "请选择" && '01'==nbPaymentMethod){
		alertMsg("请选择开户行");
		return ;
	}
	
	//判断是否选择省市
	var bankProvinces = $('#bankProvinces option:selected') .val();
	if(bankProvinces == "请选择" && '01'==nbPaymentMethod){
		alertMsg("请选择银行开户地");
		return ;
	}
	
	//判断是否选择省市
	var bankCities = $('#bankCities option:selected') .val();
	if(bankProvinces == "请选择" && '01'==nbPaymentMethod){
		alertMsg("请选择省（直辖市）");
		return ;
	}
	
	//判断是否填写银行卡
	var pmBaNov = $("#pmBaNo").val();
	if(pmBaNov == "" && '01'==nbPaymentMethod){
		alertMsg("请填写银行账号");
		return ;
	}
	
	saveEditPaygo();
}

function saveEditPaygo(){
	//判断开户行
	var pmBankCategories = $('#pmBankCategories option:selected').text();
	//判断是否选择省市
	var bankProvinces = $('#bankProvinces option:selected').text();
	//判断是否选择省市
	var bankCities = $('#bankCities option:selected').text();
	//判断是否填写银行卡
	var pmBaNov = $("#pmBaNo").val();
	
	var nbPaymentMethod = $("#nbPaymentMethods").val();
	
	if('01' == nbPaymentMethod){
		alertMsg("您的首期保费交费银行帐户将改为 <br/>"+ 
				"&nbsp;&nbsp;&nbsp; 开户行："+ pmBankCategories +"<br/>"+ 
				"&nbsp;&nbsp;&nbsp; 开户地："+ bankProvinces +"<br/>"+ 
				"省直辖市："+ bankCities +"<br/>"+
				"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp账号："+ pmBaNov +"<br/>"
				 ,1,function(){
					appBase.nbPaymentMethod = $("#nbPaymentMethods").val();
					appBase.pmBaBankCategoryId = $("#pmBankCategories").val(); 
					appBase.pmBaNo = $("#pmBaNo").val();
					appBase.pmBaProvince = $("#bankProvinces").val();
					appBase.pmBaAreaCode = $("#bankCities").val();
					var sendData = appBase;
					padMarketing.ajaxNormal('saveEditPayTry.action',sendData,
				    		function(result) {
				    			var status = result.status;
				    			var errMsg = result.errMsg;
				    			if('F'==status){
				    				alertMsg(errMsg);
				    			}else{
					        		var json = JSON.parse(result.data);
					        		var result = json.result;
					        		var errMsg = json.errMsg;
						 			if('0'==result){//不需要签字
						 				if($("#nbPaymentMethods").val() == "02"){
						 					//跳转在线支付宝微信支付
						 					window.location='/pad_marketing/main/pay.action?url=ordermanager/index.action';
						 				}else{
						 					//跳转DDA实时代扣
						 					window.location='/pad_marketing/main/ddapay.action?url=ordermanager/index.action';
						 				}
						 				//window.location='../ordermanager/index.action';
						 			}else if('1'==result){//错误信息
						 				alertMsg(errMsg);
						 			}else if('2'==result){//需要签字
						 				sessionStorage.setItem("editPayAppBase",JSON.stringify(appBase));
						 				window.location='bankAuth.action';
						 			}
				    			}
				    			
				         	},function(result) {
			//	         		alertMsg("保存支付方式异常！");
				         	},
				         	true,//是否异步
							true,//是否显示加载电波图
							'加载中···'//电波图中的话术
				         	
				    	);
				});
		
	}else{
		appBase.nbPaymentMethod = $("#nbPaymentMethods").val();
		appBase.pmBaBankCategoryId = $("#pmBankCategories").val(); 
		appBase.pmBaNo = $("#pmBaNo").val();
		appBase.pmBaProvince = $("#bankProvinces").val();
		appBase.pmBaAreaCode = $("#bankCities").val();
		var sendData = appBase;
		padMarketing.ajaxNormal('saveEditPayTry.action',sendData,
	    		function(result) {
	    			var status = result.status;
	    			var errMsg = result.errMsg;
	    			if('F'==status){
	    				alertMsg(errMsg);
	    			}else{
		        		var json = JSON.parse(result.data);
		        		var result = json.result;
		        		var errMsg = json.errMsg;
			 			if('0'==result){//不需要签字
			 				window.location='/pad_marketing/main/pay.action?url=ordermanager/index.action';
			 				//window.location='../ordermanager/index.action';
			 			}else if('1'==result){//错误信息
			 				alertMsg(errMsg);
			 			}else if('2'==result){//需要签字
			 				sessionStorage.setItem("editPayAppBase",JSON.stringify(appBase));
			 				window.location='bankAuth.action';
			 			}
	    			}
	    			
	         	},function(result) {
//	         		alertMsg("保存支付方式异常！");
	         	},
	         	true,//是否异步
				true,//是否显示加载电波图
				'加载中···'//电波图中的话术
	         	
	    	);
		
	}
	
}



/**
 * 在线支付隐藏下面
 */
function paymentMethod(){
	var nbPaymentMethod = $("#nbPaymentMethods").val();
	if('02'==nbPaymentMethod){
		if(nbPremium>20000){
			alertMsg("在线支付的限额不能超过20000元");
			$("#nbPaymentMethods").val("00");
		} else {
			$("#bankPay").hide();
		}
	}else{
		$("#bankPay").show();
	}
}


